package com.sanpu.kxe.business.member.presenter;

import android.app.Activity;
import android.text.TextUtils;

import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.provider.IMainProvider;
import com.sanpu.kxe.business.member.bean.MemberLevelOutput;
import com.sanpu.kxe.business.member.contract.MemberLevelsListContract;
import com.sanpu.kxe.business.member.interfaces.IMemberLevelListener;
import com.sanpu.kxe.business.member.model.MemberModel;
import com.sanpu.kxe.business.member.provider.MemberOpenProvider;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.DialogViewListener;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.utils.DialogUtil;
import com.sanpu.kxe.utils.PublicProviderUtil;
import com.sanpu.kxe.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：会员等级列表
 * Created by 薛勇军 on 2018/7/30.
 */

public class MemberLevelsListPresenter implements MemberLevelsListContract.Presenter {

    private MemberLevelsListContract.View mView;

    private CompositeSubscription mSubscription;

    private List<MemberLevelOutput> mLevelOutputs = new ArrayList<>();

    public MemberLevelsListPresenter(MemberLevelsListContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public IMemberLevelListener getLevelListener() {
        return new IMemberLevelListener() {
            @Override
            public void onChangeAgreementClick(int curPosition, boolean isChoose) {
                if (curPosition < mLevelOutputs.size()) {
                    mLevelOutputs.get(curPosition).setChoose(!isChoose);
                    mView.showMemberList(mLevelOutputs);
                }
            }

            @Override
            public void onAgreementClick() {
                IMainProvider provider = PublicProviderUtil.getProvider(IMainProvider.class);
                if (provider != null) {
                    provider.openBrowserX5Activity(mView.getContext(), UrlConstants.URL_UPGRADE_AGREEMENT, true);
                }
            }

            @Override
            public void onGradeMemberClick(MemberLevelOutput levelOutput) {
//                if (!isAuthenPass()) return;
                if (levelOutput != null) {
                    if (levelOutput.isAgreement() && !levelOutput.isChoose()) {
                        ToastUtil.show("请先阅读并同意协议！");
                        return;
                    }
                    MemberOpenProvider.getInstance().openMemberCashierActivity(mView.getContext(),
                            levelOutput.getRechargeAmount(), levelOutput.getLevelName(), levelOutput.getVipOpenId());
                }
            }
        };
    }

    @Override
    public void findVipConfList() {
        mView.showLoadingDialog(true);
        Subscription subscription = MemberModel.getInstance().findVipConfList()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<MemberLevelOutput>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(List<MemberLevelOutput> memberLevelOutputs) {
                        if (memberLevelOutputs != null) {
                            mLevelOutputs.clear();
                            mLevelOutputs.addAll(memberLevelOutputs);
                            mView.showMemberList(memberLevelOutputs);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void openCertificationActivity() {
        IMainProvider mainProvider = PublicProviderUtil.getProvider(IMainProvider.class);
        if (mainProvider != null) {
            mainProvider.openCertificationActivity(mView.getContext(), true);
        }
    }

    /**
     * 是否认证
     */
    public boolean isAuthenPass() {
        final UserCenterOutput centerOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (centerOutput != null) {
            if (!TextUtils.equals(CommonConfig.AUTHEN_PASS, centerOutput.getChkSts())) {
                String btnText = "", contentText = "";
                switch (centerOutput.getChkSts()) {
                    case CommonConfig.AUTHEN_NOTSUB:
                        btnText = "去认证";
                        contentText = "升级会员前请先完成认证";
                        break;
                    case CommonConfig.AUTHEN_AUDIT:
                        btnText = "确定";
                        contentText = "您的实名认证正在审核中，请稍后";
                        break;
                    case CommonConfig.AUTHEN_NOPASS:
                        btnText = "重新认证";
                        contentText = "您的实名认证审核失败，请重新认证";
                        break;
                    case CommonConfig.AUTHEN_NOCOMPLETION:
                        btnText = "去补全";
                        contentText = "需要补全资料才能升级会员";
                        break;
                }
                DialogUtil.getInstance().dialogNoTitletTwoBtnHaveCallBack(mView.getContext(),
                        contentText, "取消", btnText, new DialogViewListener() {
                            @Override
                            public void btnRightConfirm() {
                                if (!TextUtils.equals(CommonConfig.AUTHEN_AUDIT, centerOutput.getChkSts())) {
                                    openCertificationActivity();
                                } else {
                                    ((Activity) mView.getContext()).finish();
                                }
                            }

                            @Override
                            public void btnLeftCancel() {
                                ((Activity) mView.getContext()).finish();
                            }
                        });
                return false;
            }
            return true;
        }
        return false;
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
